package coms;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 各种日期格式字符串
 * @author CHENHAILIAN6
 *
 */
public class TestDateString {

	public static void main(String[] args) {
	
		/*Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
	    String dateString = formatter.format(currentTime);  
	    System.out.println(dateString);
	    FileWriter writer = null;
	    try {
	    	//打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
	    	String fileName = "d:/documents/testdatas.txt";
	    	writer = new FileWriter(fileName, true);
	    	writer.write(dateString+"\r\n");
	    	writer.close();
	    } catch (IOException e) {
	    	if(writer !=null) {
	    		try {
					writer.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	    	}
	      e.printStackTrace();
	   }
	    System.out.println(TestDateString.afterYearDay(2016, 2,27));
	    System.out.println(TestDateString.afterDay(-1));
	    System.out.println(TestDateString.today());
	    
	   
		
		String[] testStrings = new String[] {"1","2","3"};
		System.out.println(TestDateString.arrayToString(testStrings));*/
	 
	/*  System.out.println(TestDateString.getTime());*/
	  String  s1 = "3001,4001";
	  String s2 = "5003001,4001";
	  System.out.println(s2.contains(s1));
	  
	}
	
	public static boolean isTodayWorkDay() {
	  boolean isworked = true;
	  Calendar c = Calendar.getInstance();
      java.util.Date mydate = new java.util.Date();
      c.setTime(mydate);
      int weekDay = c.get(Calendar.DAY_OF_WEEK);
      if(weekDay>0 && weekDay < 6) {
        
      }else {
        isworked = false;
      }
      
      return isworked;
	}
	
	public static String getDateString() {
	  Date currentTime = new Date();  
      SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
      String dateString = formatter.format(currentTime);  
      return dateString;
	}
	
	public static String getTime() {
	  Date currentTime = new Date();  
      SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");  
      String dateString = formatter.format(currentTime);  
      return dateString;
	}
	
	public static String today() {
		Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(currentTime);  
	    return dateString;
	}
	
	public static String now() {
      Date currentTime = new Date();  
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
      String dateString = formatter.format(currentTime);  
      return dateString;
  }
	public static String afterDayTime(int i) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
         
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, +i);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("过去七天："+day);
        
        return day;
	}

	public static String afterDay(int i) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
         
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, +i);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("过去七天："+day);
        
        return day;
	}
	
	public static String afterMonthTimes(int i) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
         
        c.setTime(new Date());
        c.add(Calendar.MONTH, +i);
        Date m = c.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月："+mon);
        
        return mon;
	}
	
	public static String afterMonthDay(int i) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
         
        c.setTime(new Date());
        c.add(Calendar.MONTH, +i);
        Date m = c.getTime();
        String mon = format.format(m);
        
        return mon;
	}
	
	public static String afterYearDay(int year,int month,int day) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
         
        c.setTime(new Date());
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month-1);
       // c.add(Calendar.MONTH, +month);
        c.set(Calendar.DATE, day-1);
        Date m = c.getTime();
        String mon = format.format(m);
        
        return mon;
	}
	
	/**
	 * 2018/08/08 - 2018/09/29
	 * @param date
	 * @return "2018-08-07T16:00:00.000Z","2018-09-28T16:00:00.000Z"
	 */
	public static String getcalendarString(String date) {
		String dateString;
		String[] times = date.split("-");
		String fromDate = times[0].trim();
		String toDate = times[1].trim();
		String[] numbers = fromDate.split("/");
		String year = numbers[0];
		String month = numbers[1];
		String day = numbers[2];
		if(month.startsWith("0")) {
			month = month.replaceFirst("0","");
		}
		if(day.startsWith("0")) {
			day = day.replaceFirst("0","");
		}
		String startDate = afterYearDay(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));
		numbers = toDate.split("/");
		year = numbers[0];
		month = numbers[1];
		day = numbers[2];
		if(month.startsWith("0")) {
			month = month.replaceFirst("0","");
		}
		if(day.startsWith("0")) {
			day = day.replaceFirst("0","");
		}
		String endDate = afterYearDay(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));
		date = startDate+"T16:00:00.000Z";
		
		return date;
	}
	
	
	/**
	 * 2018/08/08
	 * @param date
	 * @return 2018-08-07T16:00:00.000Z
	 */
	public static String getcalendarSingleString(String date) {
		String dateString;
		String[] numbers = null;
		if(date.contains("/")) {
			numbers = date.split("/");
		}else if(date.contains("-")) {
			numbers = date.split("-");
		}
		String year = numbers[0];
		String month = numbers[1];
		String day = numbers[2];
		if(month.startsWith("0")) {
			month = month.replaceFirst("0","");
		}
		if(day.startsWith("0")) {
			day = day.replaceFirst("0","");
		}
		String startDate = afterYearDay(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));
	/*	numbers = toDate.split("/");
		year = numbers[0];
		month = numbers[1];
		day = numbers[2];
		if(month.startsWith("0")) {
			month = month.replaceFirst("0","");
		}
		if(day.startsWith("0")) {
			day = day.replaceFirst("0","");
		}
		String endDate = afterYearDay(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));*/
		dateString = startDate+"T16:00:00.000Z";
		
		return dateString;
	}
	
	/**
	 * 将数组转换成字符串
	 * @return
	 */
	public static String arrayToString(Object[] values) {
	      
	  int size = values.length;
      String temp = "";
      for(int i=0;i<size;i++) {
          if (!temp.equals("")) {
              temp += "," + String.valueOf(values[i]);
          } else {
                  temp = String.valueOf(values[i]);
          }
        }
      String reusltToString = "["+temp+"]";
      return reusltToString;
	}
	
}
